using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums;
using AspNetForums.Components;

namespace AspNetForums.Admin {
	/// <summary>
	/// Summary description for ManageRanks.
	/// </summary>
	public class ManageRanks : System.Web.UI.Page {
		protected System.Web.UI.WebControls.DataList dlManageRanks;
	
		private void Page_Load(object sender, System.EventArgs e) {

			this.dlManageRanks.EditCommand +=new DataListCommandEventHandler(dlManageRanks_EditCommand);
			this.dlManageRanks.CancelCommand +=new DataListCommandEventHandler(dlManageRanks_CancelCommand);
			this.dlManageRanks.DeleteCommand +=new DataListCommandEventHandler(dlManageRanks_DeleteCommand);
			this.dlManageRanks.UpdateCommand +=new DataListCommandEventHandler(dlManageRanks_UpdateCommand);
			this.dlManageRanks.ItemCommand +=new DataListCommandEventHandler(dlManageRanks_ItemCommand);

			// Put user code to initialize the page here
			this.dlManageRanks.DataSource = Ranks.GetRanks();

			if( !Page.IsPostBack)
				dlManageRanks.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e) {
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		private void dlManageRanks_EditCommand(object source, DataListCommandEventArgs e) {
			this.dlManageRanks.EditItemIndex = e.Item.ItemIndex;
			dlManageRanks.DataBind();
		}

		private void dlManageRanks_CancelCommand(object source, DataListCommandEventArgs e) {
			this.dlManageRanks.EditItemIndex = -1;
			dlManageRanks.DataBind();
		}

		private void dlManageRanks_DeleteCommand(object source, DataListCommandEventArgs e) {
			Rank rank = (Rank)((ArrayList)dlManageRanks.DataSource)[e.Item.ItemIndex];


			dlManageRanks.EditItemIndex = -1;
			dlManageRanks.DataBind();
		}

		private void dlManageRanks_UpdateCommand(object source, DataListCommandEventArgs e) {
			TextBox txtRankName		= (TextBox)e.Item.FindControl("txtRankName");
			TextBox txtRankCountMin	= (TextBox)e.Item.FindControl("txtRankMinPost");
			TextBox txtRankCountMax	= (TextBox)e.Item.FindControl("txtRankMaxPost");
			TextBox	txtRankIconUrl	= (TextBox)e.Item.FindControl("txtRankIconUrl");

			if( txtRankName		== null 
			||	txtRankCountMin	== null 
			||	txtRankCountMax	== null 
			||	txtRankIconUrl	== null )
				return;

			AspNetForums.Components.Rank r = (Rank)((ArrayList)dlManageRanks.DataSource)[e.Item.ItemIndex];

			r.RankName				= txtRankName.Text;
			r.PostingCountMinimum	= Int32.Parse(txtRankCountMin.Text);
			r.PostingCountMaximum	= Int32.Parse(txtRankCountMax.Text);
			r.RankIconUrl			= txtRankIconUrl.Text;

			Ranks.UpdateRank( r );

			dlManageRanks.EditItemIndex = -1;
			dlManageRanks.DataBind();
		}

		private void dlManageRanks_ItemCommand(object source, DataListCommandEventArgs e) {
			if( e.CommandName == "Create" )
				CreateCommand( source, e );
	
		}

		private void CreateCommand( object source, DataListCommandEventArgs e ) {
			TextBox txtRankName	= (TextBox)e.Item.FindControl("txtNewRankName");
			TextBox txtRankCountMin	= (TextBox)e.Item.FindControl("txtNewRankMinPost");
			TextBox txtRankCountMax = (TextBox)e.Item.FindControl("txtNewRankMaxPost");
			TextBox txtRankIconUrl	= (TextBox)e.Item.FindControl("txtNewRankIconUrl");

			if( txtRankName		== null 
				||	txtRankCountMin	== null 
				||	txtRankCountMax	== null 
				||	txtRankIconUrl	== null )
				return;

			Rank rank = new Rank( 0, txtRankName.Text, Int32.Parse( txtRankCountMin.Text), Int32.Parse( txtRankCountMax.Text ), txtRankIconUrl.Text );

			Ranks.CreateRank( rank );
			
			DataBind();
		}
	}
}
